package com.note.simpleshop.module.cart.controller;

import com.note.simpleshop.module.cart.service.CartService;
import com.note.simpleshop.constant.ErrorCode;
import com.note.simpleshop.entity.TbCart;
import com.note.simpleshop.exception.NotDataException;
import com.note.simpleshop.module.user.auth.RequestToken;
import com.note.simpleshop.module.user.auth.UserUtils;
import com.note.simpleshop.vo.Msg;
import com.note.simpleshop.vo.PageBean;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("cart/cart")
public class CartController {
    @Autowired
    private CartService cartService;

    @ApiOperation(value = "add", notes = "加入购物车")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header")})
    @PostMapping(value = "add")
    @RequestToken
    public Msg addCart(TbCart model) {
        model.setUserId(UserUtils.currentUserId());
        Msg msg = new Msg();
        try {
            if (model == null) {
                msg.setErrorCode(ErrorCode.ERROR_1000000);
                msg.setMsg("数据异常");
                return msg;
            } else if (StringUtils.isBlank(model.getGoodsSpecId())) {
                msg.setErrorCode(ErrorCode.ERROR_1000000);
                msg.setMsg("goodsSpectId不为空");
                return msg;
            } else if (StringUtils.isBlank(model.getGoodsId())) {
                msg.setErrorCode(ErrorCode.ERROR_1000000);
                msg.setMsg("goodsId不为空");
                return msg;
            } else if (null != model.getNum() && model.getNum() <= 0) {
                msg.setErrorCode(ErrorCode.ERROR_1000000);
                msg.setMsg("num不为空且大于0");
                return msg;
            } else if (model.getPrice() == null) {
                msg.setErrorCode(ErrorCode.ERROR_1000000);
                msg.setMsg("price不为空");
                return msg;
            }
            cartService.addCart(model);
            msg.setErrorCode(ErrorCode.ERROR_0000);
            msg.setMsg("ok");
        } catch (NotDataException e) {
            msg.setErrorCode(ErrorCode.ERROR_1000000);
            msg.setMsg("notDataException");
        }
        return msg;
    }

    @ApiOperation(value = "update", notes = "更新购物车商品数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header")})
    @PostMapping(value = "update")
    @RequestToken
    public Msg updateCart(String cartId, Integer num) {
        String currentUserId = UserUtils.currentUserId();
        Msg msg = new Msg();
        try {
            if (StringUtils.isBlank(cartId)) {
                msg.setErrorCode(ErrorCode.ERROR_1000000);
                msg.setMsg("cartId不为空");
                return msg;
            } else if (null != num && num <= 0) {
                msg.setErrorCode(ErrorCode.ERROR_1000000);
                msg.setMsg("num不为空且大于0");
                return msg;
            }
            cartService.updateCart(currentUserId, cartId, num);
            msg.setErrorCode(ErrorCode.ERROR_0000);
            msg.setMsg("ok");
        } catch (NotDataException e) {
            msg.setErrorCode(ErrorCode.ERROR_1000000);
            msg.setMsg(e.getMessage());
        } catch (Exception e) {
            msg.setErrorCode(ErrorCode.ERROR_1000001);
            msg.setMsg("系统异常");
        }
        return msg;
    }

    @ApiOperation(value = "list", notes = "获取购物车列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber", value = "pageNumber", required = false, paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header")})
    @PostMapping(value = "list")
    @RequestToken
    public Msg list(Integer pageNumber) {
        Msg msg = new Msg();
        try {
            if (pageNumber == null || pageNumber < 1) {
                msg.setErrorCode(ErrorCode.ERROR_1000000);
                msg.setMsg("pageNumber不能空且大于等于1");
                return msg;
            } else {
                String currentUserId = UserUtils.currentUserId();
                PageBean page = cartService.findByPage(pageNumber, currentUserId);
                msg.setErrorCode(ErrorCode.ERROR_0000);
                msg.setData(page);
                return msg;
            }
        } catch (Exception e) {
            msg.setErrorCode(ErrorCode.ERROR_1000001);
            msg.setMsg(e.getMessage());
        }
        return msg;
    }
    @ApiOperation(value = "delete", notes = "删除购物车")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header")})
    @PostMapping(value = "delete")
    @RequestToken
    public Msg deleteCart(String cartId) {
        String currentUserId = UserUtils.currentUserId();
        Msg msg = new Msg();
        try {
            if (StringUtils.isBlank(cartId)) {
                msg.setErrorCode(ErrorCode.ERROR_1000000);
                msg.setMsg("cartId不为空");
                return msg;
            }
            cartService.deleteCart(currentUserId, cartId);
            msg.setErrorCode(ErrorCode.ERROR_0000);
            msg.setMsg("ok");
        } catch (NotDataException e) {
            msg.setErrorCode(ErrorCode.ERROR_1000000);
            msg.setMsg(e.getMessage());
        } catch (Exception e) {
            msg.setErrorCode(ErrorCode.ERROR_1000001);
            msg.setMsg("系统异常");
        }
        return msg;
    }




}
